
<template>
  <div class="table-box">
    <!-- 查询表单 -->
    <SearchForm
      v-show="isShowSearch"
      @search="search"
      @reset="reset"
      :search-form-data="searchFormData"
      >
      <template #input>
        <GridItem index="1">
          <el-form-item label="项目名称" prop="project">
            <el-input v-model="searchFormData.project"></el-input>
          </el-form-item>
        </GridItem>
        <GridItem index="2">
          <el-form-item label="站点" prop="site">
            <el-input v-model="searchFormData.site"></el-input>
          </el-form-item>
        </GridItem>
        <GridItem index="3">
          <el-form-item label="时间" prop="date">
            <el-date-picker
              v-model="searchFormData.date"
              type="daterange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              @change="pickerChangeFn"
            />
          </el-form-item>
        </GridItem>
      </template>
    </SearchForm>
    <!-- 表格主体 -->
    <div class="card table-main">
      <!-- 表格头部 操作按钮 -->
      <div class="table-header">
        <div class="header-button-lf">
          
        </div>
        <div class="header-button-ri">
          <slot name="toolButton">
            <el-button icon="Search" circle @click="isShowSearch = !isShowSearch"/>
          </slot>
        </div>
      </div>
      <el-table :data="tableData" style="width: 100%" >
        <el-table-column type="index" label="排序" width="100" />
        <el-table-column property="project" label="项目名称" />
        <el-table-column property="site" label="站点" />
        <el-table-column property="total_count" label="计数" />
        <el-table-column property="date" label="时间">
          <template #default="scope">
            <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="120">
          <template #default="scope">
            <div class="option-btn">
              <!-- 查看 -->
              <el-icon class="option-icon" @click="view(scope.row)"><Document /></el-icon>
              <!-- 删除 -->
              <el-icon class="option-icon" @click="del(scope.row)"><Delete /></el-icon>
            </div>
            
          </template>
        </el-table-column>
      </el-table>
      <!-- <Pagination :total="total" :currentPage="listQuery.page" :pageSize="listQuery.pageSize" @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" /> -->
    </div>
    <Cost ref="CostRef"></Cost>
  </div>
</template>

<script setup lang="ts" name="strong">
import { ref, reactive, onMounted, watch, onUnmounted, computed } from "vue";
import GridItem from "@/components/Grid/components/GridItem.vue";
// 搜索
import SearchForm from "@/components/SearchForm/index.vue";
import { summariesListApi,summariesInfoApi,summariesDelApi} from "@/api/modules/daily";
// 分页
// import Pagination from "@/components/Pagination/index.vue";
// 新增、编辑、查看
import Cost from "./components/cost.vue";
import { parseTime } from "@/utils/index";
import { ElMessage,ElMessageBox } from "element-plus";

// 搜索数据
const searchFormData = reactive({
  project:'',
  site:'',
  date:[],
  date_range_start:'',
  date_range_end:'',
});
// 是否显示搜索模块
const isShowSearch = ref(true);
watch(() => searchFormData.date, () => {
  searchFormData.date_range_start = parseTime(searchFormData.date[0],'{y}-{m}-{d}')
  searchFormData.date_range_end = parseTime(searchFormData.date[1],'{y}-{m}-{d}')
})
const pickerChangeFn = (value) => {
  if(value===null){
    searchFormData.date_range_start = ''
    searchFormData.date_range_end = ''
  }
}

const listQuery = reactive({
  page: 1, // 当前页数
  pageSize: 20, // 每页显示条数
  order: 'desc',
  column: 'createdAt'
})
// 总条数
const total = ref(0)
// 每页条数发生改变
const handleSizeChange = (val) => {
  listQuery.pageSize = val
  getTableList()
}
// 页码发生改变
const handleCurrentChange = (val) => {
  listQuery.page = val
  getTableList()
}

onMounted(() => {
  getTableList()
});
let tableData = ref()
const getTableList = () => {
  let { date, ...newObj } = searchFormData;
  summariesListApi({...newObj}).then((res:any) => {
    tableData.value = res
    // total.value = res.data.totalCount
  })
};
const search = () => {
  getTableList()
};
const reset = () => {
  searchFormData.date = []
  searchFormData.date_range_start = ''
  searchFormData.date_range_end = ''
  getTableList()
};
const CostRef = ref()
const view = (row) => {
  CostRef.value.open({id:row._id})
}
const del = (row) => {
  ElMessageBox.confirm('确定要删除吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    summariesDelApi(row._id).then((res:any) => {
      if(res.success) {
        ElMessage.success('删除成功！')
        getTableList()
      }
    })
  })
}
</script>
<style lang="scss" scoped>
.option-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  .option-icon {
    margin: 0 10px;
    cursor: pointer;
  }
}
</style>
